//
//  ZZHomeListCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/6/14.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZHomeListCell.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import "ZZShareView.h"
#import "MWPhotoBrowser.h"
#import "UserInfoController.h"
#import "ZCTTMLEmojiLabel.h"
#import "SVWebViewController.h"
#import "ZZContentDetailController.h"
@interface ZZHomeListCell()<ZCTTMLEmojiLabelDelegate,UIGestureRecognizerDelegate>{
    
}
@end

@implementation ZZHomeListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    NSLog(@"初始化----");
    
    // Initialization code
    _imgAvatar.layer.cornerRadius = 22.0f;
    _imgAvatar.layer.masksToBounds = YES;
    
    [_labTitle setFont:FontFiftent];
    _labTitle.numberOfLines = 0;
    [_labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    
    [_labName setFont:FontFiftent];
    [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    
    [_btnLevel.titleLabel setFont:ListElevenFont];
    _btnLevel.layer.cornerRadius = 4.0f;
    _btnLevel.layer.masksToBounds = YES;
    _btnLevel.userInteractionEnabled = NO;
    _btnVip.userInteractionEnabled = NO;
    [_btnLevel setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    //[_btnLevel setImage:[UIImage imageNamed:@"收藏 小按钮"] forState:0];
    
    //[_btnVip setBackgroundImage:[UIImage imageNamed:@"vip"] forState:0];
    
    [_labType setFont:FontThirteen];
    [_labType setTextColor:UIColorFromRGB(BgTitleColor)];
    
    [_labTime setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(BgLineColor)];
    
    [self setBtnStype:_btnFav image:@"Combined_Shape"];
    [self setBtnStype:_btnComment image:@"评论 小按钮"];
    [self setBtnStype:_btnStar image:@"收藏 小按钮"];
    
    [self setBtnStype:_btnShare image:@"icon_sharenew"];
    [_btnShare setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnShare setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    
    _viewContent.userInteractionEnabled = YES;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    
    _imgAvatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_imgAvatar addGestureRecognizer:tap1];
    
    _labName.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarClick:)];
    [_labName addGestureRecognizer:tap2];
    
    _labTitle.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(copyMessage:)];
    [_labTitle addGestureRecognizer:lp];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClick:)];
    [_labTitle addGestureRecognizer:tap];
    
    [_btnDel setImage:[UIImage imageNamed:@"发布 关闭 标签"] forState:0];
    [_btnDel setTitle:@"" forState:0];
    _btnDel.hidden = YES;
    [_btnDel addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setBtnStype:(UIButton *) btn image:(NSString *)imgName{
    [btn setImage:[UIImage imageNamed:imgName] forState:0];
    [btn setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:0];
    [btn.titleLabel setFont:ListElevenFont];
    [btn setTitle:@"" forState:0];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 2, 0, 0)];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn setBackgroundColor:UIColor.clearColor];
    [btn addTarget:self action:@selector(onCotrolClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.selected = NO;
    btn.userInteractionEnabled = YES;
}

-(void)avatarClick:(UITapGestureRecognizer *) tap{
    UserInfoController *vc = [[UserInfoController alloc] init];
    vc.user_id = _tempModel.user_info.user_id;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
    }
}
-(void)titleClick:(UITapGestureRecognizer *)tap{
    ZZHomeTopicEntity *model= _tempModel;
    ZZContentDetailController *vc = [[ZZContentDetailController alloc] init];
    vc.entity = model;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
    }
}
-(void)onCotrolClick:(UIButton *)btn{
    ZZTopicCellClickType type = 0;
    if([btn isEqual:_btnShare]){
        type = ZZClickShare;
        _btnShare.selected = YES;
    }else if([btn isEqual:_btnFav]){
        type = ZZClickFav;
        _btnFav.selected = YES;
    }else if([btn isEqual:_btnStar]){
        type = ZZClickStar;
        _btnStar.selected = YES;
        
//        [PopUtil showMessage:@"收藏要给小心心这样才能亮晶晶"];
    }else if([btn isEqual:_btnComment]){
        type = ZZClickComment;
        _btnComment.selected = YES;
    }else if([btn isEqual:_btnDel]){
        type = ZZClickDel;
    }
    
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:type obj:self.tempModel];
    }
}


-(void) moreComment:(UITapGestureRecognizer *) tap{
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickComment obj:self.tempModel];
    }
}


-(void)dataToView:(ZZHomeTopicEntity *) topic{
    self.tempModel = topic;
    self.backgroundColor = UIColor.clearColor;
    [self setUserViewData:topic];
    [_imgBgView setBackgroundColor:UIColor.whiteColor];
    _imgBgView.layer.cornerRadius = 10.0f;
    _imgBgView.layer.masksToBounds = YES;
    
    CGRect uf = _imgBgView.frame;
    uf.size.height = CGRectGetMaxY(_viewControl.frame) + 10;
    _imgBgView.frame = uf;
    
    self.frame = CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_imgBgView.frame));
}

-(void)setUserViewData:(ZZHomeTopicEntity *) topic{
    [_imgAvatar sd_setImageWithURL:[NSURL URLWithString:convertToString(topic.user_info.user_avatar)] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labName setText:topic.user_info.user_name];
    [_labName sizeToFit];
    //设置用户等级显示
    [self autoWidthOfLabel:_labName with:21];
    CGRect lf = _btnLevel.frame;
    lf.origin.x = CGRectGetMaxX(_labName.frame) + 5;
    _btnLevel.frame = lf;
    [self setBtnLevelColor:topic];
    
//    NSLog(@"%@是否是VIP%@",topic.user_info.user_name,topic.user_info.is_vip);
    if ([topic.user_info.is_vip intValue] == 1){
        _btnVip.hidden = NO;
        [_btnVip setImage:[UIImage imageNamed:@"vip"] forState:0];
        [_labName setTextColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0]];
    } else {
        _btnVip.hidden = YES;
        [_labName setTextColor:UIColorFromRGB(TextSizeOneColor)];
    }
    [_labTime setText:topic.add_time];
    
    [_labTitle setText:topic.title];
    
    
    
    CGSize st = [self autoWidthOfLabelHegith:_labTitle with:ScreenWidth - 40];
    
    [self addContentToView:topic fy:CGRectGetMaxY(_labTitle.frame) + ((st.height>0)?10:0)];
    
    CGRect typeF = _labType.frame;
    typeF.origin.y = CGRectGetMaxY(_viewContent.frame) + 10;
    _labType.frame = typeF;
    if(convertToString(topic.nav_name).length > 0){
        if ([topic.nav_name isEqualToString:@"签名"]){
            [_labType setText:[NSString stringWithFormat:@"个性%@",topic.nav_name]];
        } else {
            [_labType setText:topic.nav_name];
        }
       
    }else{
        [_labType setText:@"未知"];
    }
    
    [self addCommentView:topic.thread_list fy:CGRectGetMaxY(typeF)+(topic.thread_list.count == 0 ? 0 : 10)];
    
    [_btnFav setTitle:[topic.like_count intValue] == 0 ? @"赞" : topic.like_count forState:0];
    [_btnComment setTitle:topic.thread_count == 0 ?@"评论":convertIntToString((int)topic.thread_count) forState:0];
    [_btnStar setTitle:[topic.fav_count intValue] == 0 ? @"收藏" : topic.fav_count forState:0];
    
    if(topic.is_like){
        [_btnFav setImage:[UIImage imageNamed:@"ic_fav_select"] forState:0];
    }else{
        [_btnFav setImage:[UIImage imageNamed:@"ic_fav"] forState:0];
    }
    
    if(topic.is_fav){
        [_btnStar setImage:[UIImage imageNamed:@"收藏 小按钮 copy"] forState:0];
    }else{
        [_btnStar setImage:[UIImage imageNamed:@"收藏 小按钮"] forState:0];
    }
    
    CGRect cf = _viewControl.frame;
    cf.origin.y = CGRectGetMaxY(_commentView.frame);
    if(topic.thread_list.count > 0){
        cf.origin.y = cf.origin.y + 10;
    }
    _viewControl.frame = cf;
}

-(void)copyMessage:(UIGestureRecognizer *) lp{

    _labTitle.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:236.0f/255.0f blue:236.0f/255.0f alpha:1];
    
    [[AlertUtil shareInstance] showAlert:@"是否复制文字 ?" message:nil cancelTitle:@"取消" titleArray:@[@"确定"] viewController:_delegate confirm:^(NSInteger buttonTag) {
            _labTitle.backgroundColor = [UIColor whiteColor];
            if(buttonTag == 0){
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                pasteboard.string = convertToString(_tempModel.title);
                
                [PopUtil showMessage:@"已复制到剪切板"];
                
            }
        }];
    
}


-(CGFloat )createAvatarView:(ZZHomeTopicEntity *)topic{
    [_viewContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat iw = 118;//((ScreenWidth - 20)*10)/30;
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, iw, iw)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:topic.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_viewContent addSubview:imgView];
    
    
    return iw;
}


-(void)addContentToView:(ZZHomeTopicEntity *) topic fy:(CGFloat ) y{
    [_viewContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
//    if([@"sign" isEqual:topic.nav_type]){
//        UILabel *lab = [self createLabel];
//        [lab setFrame:CGRectMake(0, 0, ScreenWidth - 40, 0)];
//        [lab setFont:FontFiftent];
//        [lab setText:topic.message_1];
//        CGSize s = [self autoWidthOfLabelHegith:lab with:ScreenWidth - 40];
//        [_viewContent addSubview:lab];
//
//        CGRect f = _viewContent.frame;
//        f.origin.y = y;
//        f.size.height = s.height;
//        [_viewContent setFrame:f];
//    }else
        if([@"name" isEqual:topic.nav_type] || [@"sign" isEqual:topic.nav_type]){
        UILabel *lab = [self createLabel];
        [lab setFrame:CGRectMake(0, 0, ScreenWidth - 40, 0)];
        [lab setText:topic.message_1];
        [lab setFont:FontFiftent];
            if ([@"sign" isEqualToString:topic.nav_type]){
                [lab setTextAlignment:NSTextAlignmentLeft];
            } else {
                [lab setTextAlignment:NSTextAlignmentCenter];
            }
        CGSize s = [self autoWidthOfLabelHegith:lab with:ScreenWidth - 40];
            
        [_viewContent addSubview:lab];
        
        CGFloat itemH = s.height;
        if(convertToString(topic.message_2).length > 0){
            UIImageView *lineView = [self createImageView];
            [lineView setBackgroundColor:UIColorFromRGB(BgLineColor)];
            [lineView setFrame:CGRectMake(0, s.height + 5 + 18, ScreenWidth - 40, 1)];
            [_viewContent addSubview:lineView];
            
            UIImageView *iconView = [self createImageView];
            [iconView setFrame:CGRectMake((ScreenWidth-40 - 36)/2, s.height + 5, 36, 36)];
            [iconView setImage:[UIImage imageNamed:@"情侣图标"]];
            [_viewContent addSubview:iconView];
            
            UILabel *lab2 = [self createLabel];
            [lab2 setFrame:CGRectMake(0, s.height + 10 + 36, ScreenWidth - 40, 0)];
            [lab2 setTextAlignment:NSTextAlignmentCenter];
            [lab2 setFont:FontFiftent];
            [lab2 setText:topic.message_2];
            [self autoWidthOfLabelHegith:lab2 with:ScreenWidth - 40];
           
            [_viewContent addSubview:lab2];
            
            
            itemH = CGRectGetMaxY(lab2.frame);
        }
        CGRect f = _viewContent.frame;
        f.origin.y = y;
        f.size.height = itemH;
        [_viewContent setFrame:f];
        
    }else if([@"group" isEqual:topic.nav_type]){
        // 34 128
        
        CGFloat x = 0;//34;
        if([topic.cat_id isEqual:@"1032"]){
            x = 34;
        }
        CGFloat iy = 0;
        CGFloat itemH = 60;
        CGFloat itemW = ScreenWidth - x - 40;
        
        NSArray *arr = topic.message_arr_1;
        if(arr.count == 1){
            x = 0;
            itemW = ScreenWidth - x - 40;
        }
        for(NSString *str in arr){
            UILabel *label = [self createLabel];
            [label setFrame:CGRectMake(x+26, iy + 5, itemW - 26, 0)];
            [label setFont:FontFiftent];
            label.numberOfLines = 0;
            [label setTextAlignment:NSTextAlignmentLeft];
            [label setText:str];
            CGSize s = [self autoWidthOfLabelHegith:label with:itemW - 26];
            if(s.height > itemH){
                itemH = s.height;
            }else{
                itemH = 60;
            }
            
            [label setFrame:CGRectMake(x+26, iy+6, itemW - 26, itemH)];
          
            UIImageView *aImg = [self createImageView];
            [aImg setImage:[UIImage imageNamed:@"icon_arrow"]];
            [aImg setContentMode:UIViewContentModeCenter];
            [aImg setFrame:CGRectMake(x, iy, 16, itemH)];
            
            
            UIImageView *lineView = [self createImageView];
            [lineView setBackgroundColor:UIColorFromRGB(BgLineColor)];
            [lineView setFrame:CGRectMake(x, iy + itemH - 0.5, itemW, 1)];
            
            
            iy = iy + itemH;
            
            [_viewContent addSubview:label];
            [_viewContent addSubview:aImg];
            [_viewContent addSubview:lineView];
        }
        if(arr.count > 1 && [@"1032" isEqual:topic.cat_id]){
            UIImageView *imgView  = [self createImageView];
            [imgView setFrame:CGRectMake(0, 0, 34, iy)];
            [imgView setContentMode:UIViewContentModeCenter];
            [imgView setImage:[UIImage imageNamed:@"情侣"]];
            [_viewContent addSubview:imgView];
        }
        
        
        CGRect f = _viewContent.frame;
        f.origin.y = y;
        f.size.height = iy;
        [_viewContent setFrame:f];

    }else{
        [self addContontImages:topic fy:y];
    }
    
    
}

-(void)addContontImages:(ZZHomeTopicEntity *)topic fy:(CGFloat ) y{
    [_viewContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat maxWidth = ScreenWidth - 40;
    CGFloat space = 6;
    CGFloat minWidth = (ScreenWidth - 40 - space*2)/3;
    
    NSMutableArray *imgsObj = topic.images;
    if(imgsObj.count == 1 || [topic.nav_type isEqual:@"show"]){
        ImageEntity *firstObj = [imgsObj firstObject];
        UIImageView *imgView = [self createImageView];
        imgView.tag = 0;
        [imgView setFrame:CGRectMake(0, 0, maxWidth, maxWidth)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
        
        imgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [tap addTarget:self action:@selector(imgTouchUpInside:)];
        [imgView addGestureRecognizer:tap];
        
        if([topic.nav_type isEqual:@"show"] && imgsObj.count > 1){
            UILabel *label =  [self createLabel];
            [label setFrame:CGRectMake(10, 10, 46, 24)];
            [label setTextAlignment:NSTextAlignmentCenter];
            [label setTextColor:UIColorFromRGB(TextWhiteColor)];
            [label setBackgroundColor:UIColorFromRGBAlpha(TextSizeOneColor, 0.2)];
            [label setText:[NSString stringWithFormat:@"%zdP",imgsObj.count - 1]];
            [imgView addSubview:label];
        }
        
        [_viewContent addSubview:imgView];
    }else{
        CGFloat x = 0;
        
        for (int i = 0;i<imgsObj.count; i ++ ) {
            if(i==6){
                break;
            }
            if(i==4 && [topic.nav_type isEqual:@"pic"]){
                break;
            }
            ImageEntity *firstObj = imgsObj[i];
            UIImageView *imgView = [self createImageView];
            [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
            imgView.layer.cornerRadius = 3.0f;
            imgView.layer.masksToBounds = YES;
            imgView.tag = i;
            
            imgView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [tap addTarget:self action:@selector(imgTouchUpInside:)];
            [imgView addGestureRecognizer:tap];
            
            // 张图片居中
            if(imgsObj.count == 2){
                x = (maxWidth - minWidth * 2 - space * 2)/2;
                
                if(i==0){
                    [imgView setFrame:CGRectMake(x, 0, minWidth, minWidth)];
                }else{
                    [imgView setFrame:CGRectMake(x + space, 0, minWidth, minWidth)];
                }
            }else if(imgsObj.count == 4 || [topic.nav_type isEqual:@"pic"]){
                // 张图片，第一个放大，其它居中
                if(i==0){
                    [imgView setFrame:CGRectMake(x, 0, maxWidth, 216)];
                }else{
                    [imgView setFrame:CGRectMake(x, 222, minWidth, minWidth)];
                    x = minWidth + x + space;
                }
            }else{
                // 换行，3 、4、6情况
                if(i <= 2){
                    [imgView setFrame:CGRectMake(x, 0, minWidth, minWidth)];
                }else{
                    [imgView setFrame:CGRectMake(x, minWidth + 6, minWidth, minWidth)];
                }
                
                //换行
                if(i == 2){
                    x = 0;
                }else{
                    x = minWidth + x + space;
                }
            }
            [_viewContent addSubview:imgView];
            if((imgsObj.count > 6 && i == 5) || (imgsObj.count > 4 && i==3 && [topic.nav_type isEqual:@"pic"])){
                
                UILabel *label =  [self createLabel];
                [label setFrame:imgView.frame];
                [label setTextAlignment:NSTextAlignmentCenter];
                [label setTextColor:UIColorFromRGB(TextWhiteColor)];
                if([topic.nav_type isEqual:@"pic"]){
                    [label setText:[NSString stringWithFormat:@"%zdP",imgsObj.count - 4]];
                }else{
                    [label setText:[NSString stringWithFormat:@"%zdP",imgsObj.count - 6]];
                }
                [_viewContent addSubview:label];
            }
        }
    }
    
    CGRect f = _viewContent.frame;
    f.origin.y = y;
    if(imgsObj.count == 1 || [topic.nav_type isEqual:@"show"]){
        f.size.height = maxWidth;
    }else if(imgsObj.count == 4 || (imgsObj.count > 4 && [topic.nav_type isEqual:@"pic"])){
        f.size.height = 216 + space + minWidth;
    }else{
        if(imgsObj.count <= 3){
            f.size.height = minWidth;
        }else{
            f.size.height = minWidth * 2 + space;
        }
    }
    [_viewContent setFrame:f];
}



-(UIImageView *)createImageView{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;
    return imgView;
}

-(UILabel *) createLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setNumberOfLines:0];
    return label;
}

-(void)addCommentView:(NSMutableArray *) array fy:(CGFloat) y{
    [_commentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat iy = 10;
    if(array.count == 0){
        iy = 0;
    }
    for (ZZTopicCommentEntity *entity in array) {
        ZCTTMLEmojiLabel *label = [self createLinkLabel:iy w:ScreenWidth - 60];
        
        NSString *text = [NSString stringWithFormat:@"%@:%@",entity.user_info.user_name,entity.message];
        [self addLinkMessage:label text:text len:entity.user_info];
        
        CGSize s = [self autoWidthOfLabelHegith:label with:label.frame.size.width];
        iy = s.height + iy + 7;
        [_commentView addSubview:label];
        
    }
    
    if(array.count < self.tempModel.thread_count){
        UIButton *label = [UIButton buttonWithType:UIButtonTypeCustom];
        [label setFrame:CGRectMake(10, iy, ScreenWidth - 60, 21)];
        [label setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [label.titleLabel setFont:FontThirteen];
        [label setTitleColor:UIColorFromRGB(Text6Color) forState:0];
        [label setBackgroundColor:UIColor.clearColor];
        [label setTitle:@"查看全部评论..." forState:0];
        [label addTarget:self action:@selector(moreComment:) forControlEvents:UIControlEventTouchUpInside];
        [_commentView addSubview:label];
        iy = iy + 21 + 10;
    }
    
    [_commentView setFrame:CGRectMake(10, y, ScreenWidth - 20, iy)];
    
}



/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )height{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, height);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (CGSize )autoWidthOfLabelHegith:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}



/**
 *  点击查看大图
 *
 *  @param
 */
-(void) imgTouchUpInside:(UITapGestureRecognizer *)recognizer{
    DLog(@"我怎么不处罚呢：");
    UIImageView *_picView = (UIImageView*)recognizer.view;
    int i = _picView.tag;
    if((self.tempModel.images.count > 6 && i == 5) || (self.tempModel.images.count > 4 && i==3 && [self.tempModel.nav_type isEqual:@"pic"])){
        if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
            [self.delegate onCellItemClick:ZZClickComment obj:self.tempModel];
        }
        
        return;
    }
    
    NSMutableArray *photos = [NSMutableArray new];
    for (int i = 0;i<self.tempModel.images.count; i ++ ) {
        ImageEntity *firstObj = self.tempModel.images[i];
        
        MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:firstObj.origin_image]];
        
        [photos addObject:photo];
    }
    
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithPhotos:photos];
    browser.startView = _picView;
    [browser setCurrentPhotoIndex:_picView.tag];
    browser.zoomPhotosToFill = YES;
    browser.displayNavArrows = NO;
    browser.navigationController.navigationBarHidden = YES;
    browser.displayActionButton = NO;
//    browser.enableSwipeToDismiss = YES;
    if(_delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
        [self.delegate onCellItemClick:ZZClickOpenVC obj:browser];
    }
//
//    CALayer *calayer = _picView.layer.mask;
//    [_picView.layer.mask removeFromSuperlayer];
//
//
//    NSMutableArray *photos = [[NSMutableArray alloc] init];
//
//    XHImageViewer *xh = [[XHImageViewer alloc] initWithImageViewerWillDismissWithSelectedViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
//
//    } didDismissWithSelectedViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
//
//        selectedView.layer.mask = calayer;
//        [selectedView setNeedsDisplay];
//
//
//        [photos removeAllObjects];
//    } didChangeToImageViewBlock:^(XHImageViewer *imageViewer, UIImageView *selectedView) {
//
//    }];
//
////    for (int i = 0;i<self.tempModel.images.count; i ++ ) {
//////        if(i==_picView.tag){
//////            [photos addObject:_picView];
//////        }else{
////            ImageEntity *firstObj = self.tempModel.images[i];
////            UIImageView *imgView = [self createImageView];
////            imgView.tag = i;
////            [imgView setFrame:_picView.frame];
////            [imgView sd_setImageWithURL:[NSURL URLWithString:firstObj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
////            [photos addObject:imgView];
//////        }
////
////    }
//    [photos addObjectsFromArray:_viewContent.subviews];
//
//    //    xh.delegate = self;
//    xh.disableTouchDismiss = NO;
//    [xh showWithImageViews:photos selectedView:_picView];
}





#pragma mark - gesture delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"ZCTTMLEmojiLabel"]) {
        return NO;
    }
    return YES;
    
}

-(ZCTTMLEmojiLabel *)createLinkLabel:(CGFloat) y w:(CGFloat ) maxWidth{
    ZCTTMLEmojiLabel *label = [[ZCTTMLEmojiLabel alloc] initWithFrame:CGRectMake(10, y, maxWidth, 0)];
    [label setTextAlignment:NSTextAlignmentLeft];
    label.numberOfLines = 0;
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setLinkColor:UIColorFromRGB(TextLinkColor)];
    [label setDelegate:self];
    label.userInteractionEnabled = YES;
    
    return label;
}

-(void)addLinkMessage:(ZCTTMLEmojiLabel *) label text:(NSString *)text len:(User *) user{
    NSString *sugguestText = [ZZCoreTools transformStringAt:text];
    NSMutableDictionary *sugguestDict = [label getTextADict:sugguestText];
    UIColor *color = UIColorFromRGB(Text6Color);
    if([user.is_vip intValue] == 1){
        color = UIColorFromRGB(BgTitleColor);
    }
    if(sugguestDict){
        [label setText:sugguestDict[@"text"] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
            //文字颜色
            [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
                                            value:color
                                            range:NSMakeRange(0, convertToString(user.user_name).length + 1)];
            return mutableAttributedString;
        }];
        NSArray *sugguestArr = sugguestDict[@"arr"];
        //    [_emojiLabel setText:tempText];
        for (NSDictionary *item in sugguestArr) {
            NSString *text = item[@"htmlText"];
            int loc = [item[@"realFromIndex"] intValue];
            // 一定要在设置text文本之后设置
            [label addLinkToURL:[NSURL URLWithString:item[@"url"]] withRange:NSMakeRange(loc, text.length)];
        }
    }else{
        if(convertToString(text).length > 0){
            NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];
            //文字颜色
            [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
                                            value:color
                                            range:NSMakeRange(0, convertToString(user.user_name).length + 1)];
            [label setAttributedText:mutableAttributedString];
        }else{
            label.text = text;
        }
    }
}

// 链接点击
-(void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url{
    DLog(@"链接地址:%@",url.absoluteString);
    [self doLinkClick:url.absoluteString];
    
}

// 链接点击
-(void)ZCMLEmojiLabel:(ZCTTMLEmojiLabel *)emojiLabel didSelectLink:(NSString *)link withType:(ZCTTMLEmojiLabelLinkType)type{
    DLog(@"意外添加的：%@",link);
    [self doLinkClick:link];
}


-(void)doLinkClick:(NSString *) url{
    if([url hasPrefix:@"name://"]){
        return;
    }
    if([url hasPrefix:@"uid://"]){
        UserInfoController *vc = [[UserInfoController alloc] init];
        vc.user_id = [url stringByReplacingOccurrencesOfString:@"uid://" withString:@""];
        if(self.delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
            [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
        }
    }else{
        if([url hasPrefix:@"tel:"] || validateNumber(url)){
            NSString *callURL = [url stringByReplacingOccurrencesOfString:@"tel:" withString:@""];
            [[AlertUtil shareInstance] showAlert:nil message:callURL cancelTitle:@"取消" titleArray:@[@"呼叫"] viewController:(UIViewController *)self.delegate confirm:^(NSInteger buttonTag) {
                if(buttonTag == 0){
                    // 打电话
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callURL]];
                }
            }];
        }else if([url hasPrefix:@"mailto:"] || validateEmail(url)){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }else{
            if (![url hasPrefix:@"https"] && ![url hasPrefix:@"http"]) {
                url = [@"https://" stringByAppendingString:url];
            }
            
            SVWebViewController *vc = [[SVWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
            if(self.delegate && [self.delegate respondsToSelector:@selector(onCellItemClick:obj:)]){
                [self.delegate onCellItemClick:ZZClickOpenVC obj:vc];
            }
        }
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//用户等级数的显示问题
-(void)setBtnLevelColor:(ZZHomeTopicEntity *) topic {
    int level = [topic.user_info.group_icon intValue];
    if (level >= 100){
        CGRect lf = _btnLevel.frame;
        lf.size.width = 75;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];
        if (level == 100){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"荣誉会员"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"荣誉会员"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 101){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"官方团队"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"官方团队"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 403){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"系统屏蔽"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"系统屏蔽"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level == 102){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"小组长"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"小组长"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level == 200){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"原创达人"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"原创达人"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level == 201){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"专题小编"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@"专题小编"] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        }
    }
    
    else {
        //等级显示处理
        CGRect lf = _btnLevel.frame;
        [self autoWidthOfLabel:_labName with:21];
        lf.origin.x = CGRectGetMaxX(_labName.frame) + 5;
        lf.size.width = 46;
        _btnLevel.frame = lf;
        [_btnVip setFrame:CGRectMake(CGRectGetMaxX(lf) + 5, lf.origin.y - 2, 52, 18)];
        if (level >= 1 && level < 6){
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"1"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch] forState:0];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 1"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv 0%@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#A4A4A4" alpha:1.0] forState:0];
        } else if (level >= 6 && level < 11) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"2"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 2"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#1BC156" alpha:1.0] forState:0];
        } else if (level >= 11 && level < 16) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"3"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 3"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#4A90E2" alpha:1.0] forState:0];
        } else if (level >= 16 && level < 21) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 4"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#8D6FFF" alpha:1.0] forState:0];
        } else if (level >= 21 && level < 26) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 5"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 26 && level < 31) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"5"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 6"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF9300" alpha:1.0] forState:0];
        } else if (level >= 31 && level < 36) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 7"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 36 && level < 41) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"8"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 8"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#3785FF" alpha:1.0] forState:0];
        } else if (level >= 41 && level < 46) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"4"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 9"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#962BFF" alpha:1.0] forState:0];
        } else if (level >= 46 && level < 51) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"10"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 10"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFA52A" alpha:1.0] forState:0];
        } else if (level >= 51 && level < 56) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"11"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]   forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 11"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FF70A6" alpha:1.0] forState:0];
        } else if (level >= 56 && level < 100) {
            [_btnLevel setBackgroundImage:[[UIImage imageNamed:@"12"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,6,0,6) resizingMode:UIImageResizingModeStretch]  forState:0 ];
            [_btnLevel setImage:[UIImage imageNamed:@"Star 12"] forState:0];
            [_btnLevel setTitle:[NSString stringWithFormat:@" Lv %@",topic.user_info.group_icon] forState:0];
            [_btnLevel setTitleColor:[UIColor colorWithHexString:@"#FFFFFF" alpha:1.0] forState:0];
        }
    }
    
}

@end
